/*
	Copyright (c) 2006, Grant Jones <grant@gxjones.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/
/*
 *  cmap.c
 *  cmap
 *
 *  Created by Grant Jones on 10/28/06.
 *
 *  Description:
 *  Simple integer mapping library. Integer key used for lookup
 *  of data in a large set -- removal and enumeration available.
 *
 *
 *  - Optimized insert over original version
 *  - Added mempool capabilties (1/22/08)
 *
 *  TODO:
 *  - Investigate using memmove instead of individual memcpy's for
 *    the removal and middle insert cases
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "cmap.h"
#ifndef NO_PAGE_SZ_ALLOC
	#include <unistd.h>
#endif

int cmap_lookup_compar(const void *a, const void *b)
{
	CMAP_KEY_TYPE ia = *((CMAP_KEY_TYPE *)a);
	CMAP_KEY_TYPE ib = *((CMAP_KEY_TYPE *)b);
	return (ia==ib?0:(ia<ib?-1:1));
}

// added 1/22/08:
// returns a newly allocate cmap from the mempool allocator; the new cmap inherits the mempool allocator
cmapStruct *cmap_new_named( mempool_t *allocator, const char *named )
{
	cmapStruct *cmap;
	if( allocator )
	{
		cmap = mempool_malloc(allocator, sizeof(cmapStruct));
		cmap_init_ex( cmap, allocator, named );
	}
	else
	{
		mempool_t *new_allocator = malloc( sizeof(mempool_t) );
		cmap = malloc(sizeof(cmapStruct));
		mempool_init_named( new_allocator, CMAP_POOL_INIT_SZ, named );
		cmap_init_ex( cmap, new_allocator, named );
		cmap->bOwnsPool = 1;	// set afterwards!
	}
		
	
	return cmap;
}
// destroys cmap
// note: only use on cmaps created with "cmap_new", this is a no-op if a mempool allocator was specified at creation
void cmap_destroy( cmapStruct *cmap )
{
	if(cmap->bOwnsPool)
	{
		mempool_destroy(cmap->mempool);
		free(cmap->mempool);		
		free(cmap);
	}
}

void cmap_init_ex( cmapStruct *cmap, mempool_t *allocator, const char *named )
{
	// added 10/29/07:
	memset( cmap, 0, sizeof(cmapStruct) );
	
	// added 1/22/08:

	if(!allocator)
	{
		cmap->mempool = NULL;
		cmap->bOwnsPool = 0;
	}
	else
	{
		cmap->mempool = allocator;
		cmap->bOwnsPool = 0;
		// cmap_new_named will set bOwnsPool
	}	

		
	
#ifdef NO_PAGE_SZ_ALLOC
	cmap->alloced = 10;
#else
	int ps = getpagesize();
	cmap->alloced = ps/CMAP_PAIR_SZ;
#endif	

//	printf("cmap alloced: %d\n", cmap->alloced );
	
//	cmap->kd = realloc( NULL, CMAP_PAIR_SZ * cmap->alloced );
//	cmap->kd = chMalloc( CMAP_PAIR_SZ * cmap->alloced );
#ifdef NO_MEMPOOL
	cmap->kd = chMalloc( CMAP_PAIR_SZ * cmap->alloced );
#else
	cmap->kd = mempool_malloc_named(cmap->mempool, CMAP_PAIR_SZ * cmap->alloced, named);
#endif
	if(!cmap->kd)
	{
		fprintf(stderr, "Failed to alloc mem in cmap: %s\n", strerror(errno) );
	}
	cmap->num = 0;


#ifdef CMAP_LOCKING
#ifdef WIN32
	InitializeCriticalSection( &cmap->lock );
#else
	// locking w/o win32
	pthread_mutex_init( &cmap->lock, NULL );
#endif
#endif

	
	
}

void cmap_duplicate( cmapStruct *cmap_dst, cmapStruct *cmap_src )
{
	if( cmap_src->num <= 0 )
	{
		cmap_dst->num = 0;
		return;
	}
	
	if( cmap_src->num > cmap_dst->alloced )
	{
		cmapStruct *cmap = cmap_dst;	// hack: cmap needs to be defined for chRealloc if we are using mempools (we just set cmap to the destination map using it's mempool if required)
		cmap_dst->alloced = cmap_src->num;
		
		cmap_dst->kd = chRealloc( cmap_dst->kd, CMAP_PAIR_SZ * cmap_dst->alloced );
		
	}
	
	cmap_dst->num = cmap_src->num;
	memcpy( cmap_dst->kd, cmap_src->kd, CMAP_PAIR_SZ * cmap_src->num );
}


#ifdef ORIGINAL
// The original:
// cmap_(re)assign
void cmap_assign( cmapStruct *cmap, CMAP_KEY_TYPE key, void *data )
{
	CMAP_PTR *found;
	CMAP_PTR *pairptr;
	
	// first, does the key exist in the map
	// if it doesn't, add it
	// otherwise, reassign the new value to the existing key
	// finally sort
	
	found = bsearch(&key, cmap->kd, cmap->num, CMAP_PAIR_SZ, cmap_lookup_compar );
	if(found)
	{
		*((void **)(found+sizeof(CMAP_KEY_TYPE))) = data;
		return;
	}
	
	if( cmap->alloced <= cmap->num )
	{
		// need to chRealloc
		cmap->alloced = cmap->num * 3;
#ifdef WIN32
		if( cmap->kd == NULL )
			cmap->kd = chMalloc( CMAP_PAIR_SZ * cmap->alloced );
		else
			cmap->kd = chRealloc( cmap->kd, CMAP_PAIR_SZ * cmap->alloced );
#else
		cmap->kd = chRealloc( cmap->kd, CMAP_PAIR_SZ * cmap->alloced );
#endif
	}
	
	// add the value:
	pairptr = cmap->kd + ( CMAP_PAIR_SZ * cmap->num );
	*((CMAP_KEY_TYPE *)pairptr) = key;
	*((void **)(pairptr+sizeof(CMAP_KEY_TYPE))) = data;
	
	cmap->num ++;

	// sort values (mergesort since the data already has order):
	mergesort(cmap->kd, cmap->num, CMAP_PAIR_SZ, cmap_lookup_compar );	

}
#else
// The new revised assign/reassign single bsearch sort on insert:
// cmap_(re)assign
void cmap_assign( cmapStruct *cmap, CMAP_KEY_TYPE key, void *data )
{
	CMAP_PTR *found;
	CMAP_PTR *pairptr;
	CMAP_PTR *endptr;
	CMAP_KEY_TYPE left, right, mid;
	
	unsigned char *base;

/*
	printf("Insert %d: \n", key );
	for( left = 0; left < cmap->num; left ++ )
	{
		printf("%d,", *((CMAP_KEY_TYPE *)&cmap->kd[left*CMAP_PAIR_SZ]) );
	}
	printf("\n");
*/	
	// first, does the key exist in the map
	// if it doesn't, add it
	// otherwise, reassign the new value to the existing key
	
	left = 0;
	right = cmap->num-1;
	base = cmap->kd;
		
	while( left <= right )
	{
		//mid = (left+right)/2;
		mid = (left+right) >> 1;
		
		if( key > *((CMAP_KEY_TYPE *)&base[mid*CMAP_PAIR_SZ]) )
		{
			left = mid + 1;
		}
		else if( key < *((CMAP_KEY_TYPE *)&base[mid*CMAP_PAIR_SZ]) )
		{
			right = mid - 1;
		}
		else
		{
			found = &base[mid*CMAP_PAIR_SZ];
			*((void **)(found+sizeof(CMAP_KEY_TYPE))) = data;
			return;
		}
	}
	
	if( cmap->alloced <= cmap->num )
	{
		// need to chRealloc
		cmap->alloced = cmap->num * 3;

		if( cmap->kd == NULL )
			cmap->kd = chMalloc( CMAP_PAIR_SZ * cmap->alloced );
		else
			cmap->kd = chRealloc( cmap->kd, CMAP_PAIR_SZ * cmap->alloced );
	}
#define OLD // for now...
#ifdef OLD
	pairptr = cmap->kd + (CMAP_PAIR_SZ*(cmap->num-1));
	endptr = cmap->kd + (CMAP_PAIR_SZ*left);
	
	while( pairptr >= endptr )
	{
		memcpy( pairptr+CMAP_PAIR_SZ, pairptr, CMAP_PAIR_SZ );
		pairptr -= CMAP_PAIR_SZ;
	}
#else
	endptr = cmap->kd + (CMAP_PAIR_SZ*left);
	memmove(	cmap->kd+(CMAP_PAIR_SZ*(left+1)), 
				endptr,
				CMAP_PAIR_SZ*(cmap->num-left-1) );

#endif

	*((CMAP_KEY_TYPE *)endptr) = key;
	*((void **)(endptr+sizeof(CMAP_KEY_TYPE))) = data;

	cmap->num ++;
}

#endif

// cmap_remove
void *cmap_remove( cmapStruct  *cmap, CMAP_KEY_TYPE key )
{
	CMAP_PTR *found;
	CMAP_PTR *pairptr;
	CMAP_PTR *endptr;
	void *data;
	found = bsearch(&key, cmap->kd, cmap->num, CMAP_PAIR_SZ, cmap_lookup_compar );
	
	if(!found)
		return NULL;
	
	data = *((void **)(found+sizeof(CMAP_KEY_TYPE)));
	
	pairptr = found + CMAP_PAIR_SZ;
	endptr = cmap->kd + (CMAP_PAIR_SZ*cmap->num);
	
	while( pairptr < endptr )
	{
		memcpy( pairptr-CMAP_PAIR_SZ, pairptr, CMAP_PAIR_SZ );
		pairptr += CMAP_PAIR_SZ;
	}
	
	cmap->num --;
	return data;
}

// cmap_lookup
void *cmap_lookup( cmapStruct *cmap, CMAP_KEY_TYPE key )
{
	CMAP_PTR *found;
	found = bsearch(&key, cmap->kd, cmap->num, CMAP_PAIR_SZ, cmap_lookup_compar );
	return (found?*((void **)(found+sizeof(CMAP_KEY_TYPE))):NULL);
}
// lookup by index (NOT key):
void *cmap_direct_lookup( cmapStruct *cmap, int index )
{
	CMAP_PTR *pairptr;
	if( !cmap || index < 0 || index > cmap->num-1 )
		return NULL;
	
	pairptr = cmap->kd + (CMAP_PAIR_SZ*index);
	
	return *((void **)(pairptr+sizeof(int)));
}

// gets the highest key, O(1) time:
CMAP_KEY_TYPE cmap_gethighestkey( cmapStruct *cmap )
{
	CMAP_PTR *pairptr = cmap->kd + (CMAP_PAIR_SZ*(cmap->num-1));
	if( cmap->num <= 0 )
		return 0;
	return *((CMAP_KEY_TYPE *)pairptr);
}

// similar to gethighestkey:
CMAP_KEY_TYPE cmap_getlowestkey( cmapStruct *cmap )
{
	CMAP_PTR *pairptr = cmap->kd;
	if( cmap->num <= 0 )
		return 0;
	return *((CMAP_KEY_TYPE *)pairptr);

}



/*
	cmap enumeartion typical usage:
	
	cmapEnumStruct e;
	CMAP_KEY_TYPE key;
	void *data;
	cmap_enum( cm, &e );
	while( data = cmap_enum_next( &e, &key ) )
	{
		// do something
	}
	
*/

void cmap_enum( cmapStruct *cmap, cmapEnumStruct *e )
{
	e->pairptr = cmap->kd;
	e->endptr = cmap->kd + (CMAP_PAIR_SZ*cmap->num);
}

void *cmap_enum_next( cmapEnumStruct *e, CMAP_KEY_TYPE *key_ptr )
{
	void *data_ptr;
	if( e->pairptr >= e->endptr )
		return NULL;
	*key_ptr = *((CMAP_KEY_TYPE *)e->pairptr);
	data_ptr = *((void **)(e->pairptr+sizeof(CMAP_KEY_TYPE)));
	e->pairptr += CMAP_PAIR_SZ;
	return data_ptr;
}

// cmap_iterate
void cmap_iterate( cmapStruct *cmap, void *ctx, void (*iterFunc)(CMAP_KEY_TYPE key, void *data, void *ctx) )
{
	CMAP_PTR *pairptr = cmap->kd;
	CMAP_PTR *endptr = cmap->kd + (CMAP_PAIR_SZ*cmap->num);
	while( pairptr < endptr )
	{
		iterFunc( *((CMAP_KEY_TYPE *)pairptr), *((void **)(pairptr+sizeof(CMAP_KEY_TYPE))), ctx );
		pairptr += CMAP_PAIR_SZ;	
	}
}

// similar to cmap_iterate but goes through from the bottom-up:
void cmap_iterate_r( cmapStruct *cmap, void *ctx, void (*iterFunc)(CMAP_KEY_TYPE key, void *data, void *ctx) )
{

	CMAP_PTR *pairptr = cmap->kd + (CMAP_PAIR_SZ*cmap->num);
	CMAP_PTR *endptr = cmap->kd;
	if(!endptr) return;	
	if( cmap->num == 0 ) return;
	do
	{
		pairptr -= CMAP_PAIR_SZ;
		iterFunc( *((CMAP_KEY_TYPE *)pairptr), *((void **)(pairptr+sizeof(CMAP_KEY_TYPE))), ctx );
	} while( pairptr != endptr );
/*
	CMAP_PTR *endptr = cmap->kd;
	CMAP_PTR *pairptr = cmap->kd + (CMAP_PAIR_SZ*(cmap->num-1));
	while( pairptr > endptr )
	{
		iterFunc( *((CMAP_KEY_TYPE *)pairptr), *((void **)(pairptr+sizeof(CMAP_KEY_TYPE))), ctx );
		pairptr -= CMAP_PAIR_SZ;	
	}
*/
}


// identical to cmap_iterate, except cmap is also passed to the iterFunc
void cmap_iterate2( cmapStruct *cmap, void *ctx, void (*iterFunc)(cmapStruct *, CMAP_KEY_TYPE key, void *data, void *ctx) )
{
	CMAP_PTR *pairptr = cmap->kd;
	CMAP_PTR *endptr = cmap->kd + (CMAP_PAIR_SZ*cmap->num);
	while( pairptr < endptr )
	{
		iterFunc( cmap, *((CMAP_KEY_TYPE *)pairptr), *((void **)(pairptr+sizeof(CMAP_KEY_TYPE))), ctx );
		pairptr += CMAP_PAIR_SZ;	
	}
}

void cmap_lock( cmapStruct *cmap )
{
#ifdef CMAP_LOCKING
#ifdef WIN32
	EnterCriticalSection( &cmap->lock );
#else
	pthread_mutex_lock( &cmap->lock );
#endif
#endif
}
void cmap_unlock( cmapStruct *cmap )
{
#ifdef CMAP_LOCKING
#ifdef WIN32
	LeaveCriticalSection( &cmap->lock );	
#else
	pthread_mutex_unlock( &cmap->lock );
#endif
#endif
}

// use deffered removals while inside of enumerate/iterate operation on the map:
void cmap_deferred_remove( cmapStruct *cmap, CMAP_KEY_TYPE key )
{
	cmapActionStruct *rms;
	rms = (cmapActionStruct *)chMalloc( sizeof(cmapActionStruct) );
	memset( rms, 0, sizeof(cmapActionStruct) );

	rms->key = key;
	rms->bRemove = 1;
//	if( cmap->deffered_removals )
	rms->link = cmap->deffered_actions;

	cmap->deffered_actions = rms;	
}

void cmap_deferred_assign( cmapStruct *cmap, CMAP_KEY_TYPE key, void *data )
{
	cmapActionStruct *rms;
	rms = (cmapActionStruct *)chMalloc( sizeof(cmapActionStruct) );
	memset( rms, 0, sizeof(cmapActionStruct) );

	rms->key = key;
	rms->data = data;	// added 1/22/08: was 100% broken, doh!
	
//	if( cmap->deffered_actions )
	rms->link = cmap->deffered_actions;
	
	cmap->deffered_actions = rms;	
}

void cmap_commit_deferred_actions( cmapStruct *cmap )
{
	cmapActionStruct *rms;
	cmapActionStruct *next_link;
	
	for( rms = cmap->deffered_actions; rms; rms = next_link )
	{
		next_link = rms->link;
		if( rms->bRemove )
			cmap_remove( cmap, rms->key );
		else
			cmap_assign(cmap,rms->key,rms->data);
		
		chFree( rms );
	}
	cmap->deffered_actions = NULL;
}

/* cmap mempool allocation assignment functions */
void *cmap_assign_new( cmapStruct *cmap, CMAP_KEY_TYPE key, size_t data_sz )
{
	// returns a newly allocated chunk of memory of data_sz size from the cmap mempool; if the cmap was not created with
	// a mempool, uses standard malloc instead (receiver is responsible for freeing the memory)
	
	void *buf = chMalloc( data_sz );
	if(!buf)
	{
		printf("malloc failed\n");

	}
	cmap_assign(cmap, key, buf);

	return buf;
}


/* Hash Functions */
#ifdef FOO
unsigned long djb2_hash(unsigned char *str)
{
/*
	http://www.cse.yorku.ca/~oz/hash.html
	djb2 is one of the best string hash functions i know. it has excellent distribution and speed on many different sets of keys and table sizes.
	this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.
*/

	unsigned long hash = 5381;
	int c;

	while (c = *str++)
		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

	return hash;
}
#endif
unsigned long djb2_hash(const char *k, unsigned long keylen)
{
	register unsigned long hash = 5381;
	const unsigned char *ptr = (unsigned char *)k;
	unsigned long i = 0;
	while (i < keylen) 
	{
		hash = hash*33 ^ *ptr;
		++ptr, ++i;
	}
	return hash;
}



/* Memory pool functions */
#ifndef MEMPOOL_REAL_CHECKSUM
#define mempool_gen_checksum(x) (0)
#else
unsigned long mempool_gen_checksum( mempool_alloc_info_t *info )
{
	int x;
	mempool_alloc_info_t v;
	unsigned char *v_ptr = (unsigned char *)&v;
	unsigned long checksum = 0x0;
	memcpy( &v, info, sizeof(mempool_alloc_info_t) );
	v.checksum = 0;
	
	for( x = 0; x < sizeof(mempool_alloc_info_t); x ++ )
		checksum += v_ptr[x];
	return checksum;
}
#endif

void mempool_integrity( mempool_t *pool )
{
	mempool_alloc_info_t *info;
	mempool_alloc_info_t *next_link;

	for( info = pool->info_head; info; info = next_link )
	{
		next_link = info->link;
		assert( info->checksum == mempool_gen_checksum( info ) );
	}
}
//void mempool_init( mempool_t *pool, size_t first_sz )
void mempool_init_named( mempool_t *pool, size_t first_sz, const char *name )
{
#ifdef MEMPOOL_SOFT_DESTROY
	pool->destroyed = 0;
#endif

	pool->next_page_id = 0;
	pool->total_alloced_sz = 0;
	pool->page_sz = first_sz;
	pool->head = NULL;	// init linked list
	pool->info_head = NULL;
	pool->free_head = NULL;
	pool->name = name;
	mempool_newpage( pool );
#ifdef DEBUG_MEMPOOL
	printf("mempool_init_named: %s\n", pool->name );
#endif
}
int mempool_newpage( mempool_t *pool )
{
	mempool_page_t *newpage = malloc( sizeof(mempool_page_t)+pool->page_sz);
	if(!newpage)
		return -1;
	mempool_assert_alive( pool );
	
	pool->total_alloced_sz += pool->page_sz;
	newpage->alloced = pool->page_sz;
	newpage->next = 0;
	newpage->link = pool->head;	
	newpage->uid = ++pool->next_page_id;
	
	pool->head = newpage;
	return 0;
}

void mempool_describe( mempool_t *pool )
{
	mempool_alloc_info_t *info;
	mempool_alloc_info_t *next_link;
	
	mempool_assert_alive( pool );
#ifdef MEMPOOL_DESC_TO_FILE	
	FILE *fp = fopen( "mempool.log", "a" );
	if(!fp)
		return;
#endif		
	for( info = pool->info_head; info; info = next_link )
	{
		next_link = info->link;
#ifdef MEMPOOL_DESC_TO_FILE			
		fprintf(fp,"allocate,%s,%ld,%ld,%ld\n", info->name, info->page_id, info->alloc_loc, info->alloc_sz );
#else
		printf("Allocation \"%s\" onto page #%ld at loc(%ld) sz(%ld)\n", info->name, info->page_id, info->alloc_loc, info->alloc_sz );
#endif
	}
	for( info = pool->free_head; info; info = next_link )
	{
		next_link = info->link;
#ifdef MEMPOOL_DESC_TO_FILE			
		fprintf(fp,"free,%s,%ld,%ld,%ld\n", info->name, info->page_id, info->alloc_loc, info->alloc_sz );
#else
		printf("Freed \"%s\" onto page #%ld at loc(%ld) sz(%ld)\n", info->name, info->page_id, info->alloc_loc, info->alloc_sz );
#endif		
	}	
#ifdef MEMPOOL_DESC_TO_FILE		
	fclose(fp);
#endif	
}

size_t mempool_locate_sz( mempool_t *pool, void *locate_ptr )
{
	mempool_alloc_info_t *info;
	
	mempool_assert_alive( pool );
		
	for( info = pool->info_head; info; info = info->link )
	{
		if( info->ptr == locate_ptr )
			return info->alloc_sz;
	}

	return 0;
}

void mempool_destroy( mempool_t *pool )
{
	mempool_page_t *page;
	mempool_page_t *next_link;
	
	mempool_assert_alive( pool );
		
	for( page = pool->head; page; page = next_link )
	{
		next_link = page->link;

//		printf("at destroy page->next: %d\n", page->next );
		
		free( page );
	}
#ifdef MEMPOOL_SOFT_DESTROY
	pool->destroyed = 1;
#endif
//	printf("mempool: total size: %ld\n", pool->total_alloced_sz );
#ifdef DEBUG_MEMPOOL
	printf("mempool_destroy: %s\n", pool->name );
#endif
}

void *mempool_malloc_named( mempool_t *pool, size_t len, const char *name )
{
	size_t page_loc;
	size_t page_id;
	void *ptr;
	
	mempool_assert_alive( pool );
		
	// allocation meta-info is alloced from pool:
	mempool_alloc_info_t *info = mempool_malloc_raw( pool, sizeof(mempool_alloc_info_t), &page_loc, &page_id );
	if(!info)
		return NULL;
	ptr = mempool_malloc_raw( pool, len, &page_loc, &page_id );
	info->name = name;	// note doesn't do a deep copy!
	info->page_id = page_id;
	info->alloc_loc = page_loc;
	info->alloc_sz = len;
	info->ptr = ptr;
	info->link = pool->info_head;
	
	info->checksum = mempool_gen_checksum( info );
	
	pool->info_head = info;
	
	return ptr;
}
void *mempool_malloc( mempool_t *pool, size_t len )
{
	mempool_assert_alive( pool );
	
	return mempool_malloc_named( pool, len, "untitled" ); 
}
void *mempool_malloc_raw( mempool_t *pool, size_t len, size_t *page_loc, size_t *page_id )
{
	void *ptr;
	
	mempool_assert_alive( pool );
		
	if( len > pool->page_sz)
	{
		// grow page size:
		while( len > pool->page_sz ) pool->page_sz += pool->page_sz;
		
		if(mempool_newpage( pool ))
		{
			printf("failed to allocate memory\n");
			return NULL;
		}
	}
	if( pool->head->next + len > pool->head->alloced )
	{
		// grow page size slower:
		pool->page_sz += pool->page_sz;
		if(mempool_newpage( pool ))
		{
			printf("failed to allocate memory\n");
			return NULL;
		}
	}
	*page_loc = pool->head->next;
	*page_id = pool->head->uid;
	ptr = &pool->head->mempool[pool->head->next];
	pool->head->next += len;
	return ptr;
}

void mempool_free( mempool_t *pool, void *buf)
{
	mempool_assert_alive( pool );

#ifndef MEMPOOL_FREE_NOOP
	mempool_alloc_info_t *info;
	mempool_alloc_info_t *info_next;	
	mempool_alloc_info_t *info_prev = NULL;
	for( info = pool->info_head; info; info = info_next )
	{
		assert( info->checksum == mempool_gen_checksum( info ) );
		
		info_next = info->link;
		if( info->ptr == buf )
		{
//			printf("found pointer in alloc list\n");
			/* remove from info head */
			if( pool->info_head == info )
			{
				pool->info_head = info_next;
			}
			else
			{
				info_prev->link = info_next;
				info_prev->checksum = mempool_gen_checksum( info_prev );
			}

			/* place into free_head */
			info->link = pool->free_head;
			pool->free_head = info;

			break;
		}
		info_prev = info;
		
	}
#endif

	return;
}

void *mempool_realloc( mempool_t *pool, void *buf, size_t len )
{
	mempool_assert_alive( pool );
	
	return mempool_realloc_named( pool, buf, len, "untitled" );
}
void *mempool_realloc_named( mempool_t *pool, void *buf, size_t len, const char *name )
{
	mempool_assert_alive( pool );
	
	/* TODO: this realloc is really dumb: it will always create a new allocation */
	
//	printf("realloc pool size is: %ld; requested new size: 0x%.8x\n", pool->total_alloced_sz, len );
	
	void *ptr = mempool_malloc_named( pool, len, name );
	if(!ptr)
		return NULL;
//	printf("ptr: 0x%.8x; buf: 0x%.8x; buf+len: 0x%.8x\n", ptr, buf, buf+len );
	
	if( buf )
	{
		size_t old_sz = mempool_locate_sz( pool, buf );
		assert( old_sz != 0 );	/* would be zero if buf wasn't allocated by the pool */
		assert( old_sz < len );	/* reallocs must grow a buffer ONLY */
	
	
		memcpy( ptr, buf, old_sz );	/* copy only the original buffer size over */
	}
	
	return ptr;
}


/* Misc string capabilities */
#ifndef WIN32
char *cmap_str_append_bytes( cmapStruct *str, const char *data, size_t data_sz )
{
	char *p = mempool_malloc_named(str->mempool, data_sz+sizeof(size_t), "append bytes");
	char *data_start;
	if(!p)
		return NULL;
	data_start = &p[sizeof(size_t)];
	*((size_t *)p) = data_sz;
	memcpy( data_start, data, data_sz );
	cmap_assign( str, str->num, p );
	return data_start;
}
char *cmap_str_concat( cmapStruct *str, const char *fmt, ... )
{
	char *p;
	char *str_start;
	va_list ap;
	char one_byte[1];
	int req_len;

	va_start(ap, fmt);
	req_len = vsnprintf(one_byte, 0, fmt, ap);
	va_end(ap);
	p = mempool_malloc_named(str->mempool, req_len+sizeof(size_t), "concat");
	if(!p)
		return NULL;

	str_start = &p[sizeof(size_t)];
	
	*((size_t *)p) = req_len;
	
	va_start(ap, fmt);
	vsnprintf(str_start, req_len+1, fmt, ap);
	va_end(ap);
	cmap_assign( str, str->num, p );
	return str_start;
} 

char *cmap_str_join( cmapStruct *str, size_t *len_out )
{
	cmapEnumStruct e;
	CMAP_KEY_TYPE key_id;
	char *str_ptr;
	int total_len = 1;	// at least a null
	char *joined_str;
	int cur_loc;
	
	cmap_enum( str, &e );
	while( (str_ptr = cmap_enum_next( &e, &key_id )) )
		total_len += *((size_t *)str_ptr);
	
	joined_str = (char *)mempool_malloc_named(str->mempool, total_len, "join" );
	if(!joined_str)
		return NULL;
	joined_str[0] = '\0';
	cmap_enum( str, &e );
	cur_loc = 0;
	while( (str_ptr = cmap_enum_next( &e, &key_id )) )
	{
		int str_part_len = *((size_t *)str_ptr);
		memcpy( &joined_str[cur_loc], &str_ptr[sizeof(size_t)], str_part_len );
		cur_loc += str_part_len;
		joined_str[cur_loc] = '\0';
	}
	
	*len_out = total_len;
	
	return joined_str;
}
#endif // #ifndef WIN32



